from rest_framework.views import APIView
# 原始APIView
from rest_framework.response import Response
# 接收请求体
from rest_framework.permissions import IsAuthenticated
# 验证用户是否登陆
from users.models import User
# 导入用户信息
from datetime import date,timedelta

from goods.models import GoodsVisitCount
from meiduo_admin.serializers.serializer import GoodsSerializer

# 用户总数统计
class TotalView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self,request):
        # 获取当前时间
        now_date = date.today()
        count = User.objects.filter(is_staff=False).count()

        return Response({
            'count':count,
            'date':now_date,
        })

# 日增用户
class UserDayCountView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self,request):
        now_date = date.today()
        count = User.objects.filter(is_staff=False,date_joined__gte=now_date).count()
        return Response({
            'count': count,
            'date': now_date,
        })

# 日活跃用户
class UserActiveCountView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self,request):
        now_data = date.today()
        count=User.objects.filter(last_login__gte=now_data).count()
        return Response({
            "count": count,
            "date": now_data
        })
# 日下单用户
class UserOrderCountView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self,request):
        now_data = date.today()
        count = User.objects.filter(orders__create_time__gte=now_data).count()
        return Response({
            'count':count,
            'date':now_data,
        })

# 月增用户
# class UserMonthCountView(APIView):
#     permission_classes = [IsAuthenticated]
#     def get(self,request):
#         now_date = date.today()
#         start_date = now_date - timedelta(days=29)
#         date_list = []
#         for i in range(30):
#             now_data1 = start_date + timedelta(days=i)
#             end_data2 = start_date + timedelta(days=i+1)
#             # __gte大于等于，__lt小于
#             count = User.objects.filter(month_joined__gte = now_data1,month_joined__lt=end_data2).count()
#             date_list.append({
#                 'date':now_date,
#                 'count':count
#             })
#         return Response(date_list)




# 日分类商品访问量

class UserMonthCountView(APIView):
    # 指定管理员权限
    permission_classes = [IsAuthenticated]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取一个月前日期
        start_date = now_date - timedelta(29)
        # 创建空列表保存每天的用户量
        date_list = []

        for i in range(30):
            # 循环遍历获取当天日期
            index_date = start_date + timedelta(days=i)
            # 指定下一天日期
            cur_date = start_date + timedelta(days=i + 1)
            # 查询条件是大于当前日期index_date，小于明天日期的用户cur_date，得到当天用户量
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=cur_date).count()

            date_list.append({
                'count': count,
                'date': index_date
            })

        return Response(date_list)

class GoodsDayView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self,request):
        # 获取当天日期
        now_date = date.today()
        # 获取当天访问的商品分类数量信息
        data = GoodsVisitCount.objects.filter(date=now_date)
        # 序列化返回分类数量
        ser = GoodsSerializer(data, many=True)

        return Response(ser.data)
